package com.bs.service.impl;

import java.util.*;
import com.bs.bean.Resource;
import com.bs.service.ResourceService;
import com.bs.mapper.*;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
@Service
/*业务层：ResourceService接口的实现类，调用ResourceDao接口的实现类对数据进行相关业务等操作*/
public class ResourceServiceImpl implements ResourceService{

//	注入ResourceDao接口的实现类对象
	@Autowired
	ResourceMapper resourceDao;
//	根据条件查询resource中的数据
	public List<Resource> findAll(Resource query) {
		// TODO Auto-generated method stub
		return findAll(query==null?new Resource():query,true);
	}
	
//	根据条件和模式查询resource中的数据
	public List<Resource> findAll(Resource whereWrap, boolean mode) {
		// TODO Auto-generated method stub
		List<Resource> list = resourceDao.selectByExample(whereWrap);
		if (whereWrap!=null) {
			Iterator<Resource> iterator = list.iterator();
			while (iterator.hasNext()) {
				Resource resource = (Resource) iterator.next();
				if (whereWrap.getId() != null&&(!(whereWrap.getId()+"").equals(""))) {
					if (mode) {
						if (!(resource.getId() + "").equals(whereWrap.getId() + "")) {
							iterator.remove();
							continue;
						}
					} else {
						if (!(resource.getId() + "").contains(whereWrap.getId() + "")) {
							iterator.remove();
							continue;
						}
					}
				}
				if (whereWrap.getFile() != null&&(!(whereWrap.getFile()+"").equals(""))) {
					if (mode) {
						if (!(resource.getFile() + "").equals(whereWrap.getFile() + "")) {
							iterator.remove();
							continue;
						}
					} else {
						if (!(resource.getFile() + "").contains(whereWrap.getFile() + "")) {
							iterator.remove();
							continue;
						}
					}
				}
				if (whereWrap.getInstrumentid() != null&&(!(whereWrap.getInstrumentid()+"").equals(""))) {
					if (mode) {
						if (!(resource.getInstrumentid() + "").equals(whereWrap.getInstrumentid() + "")) {
							iterator.remove();
							continue;
						}
					} else {
						if (!(resource.getInstrumentid() + "").contains(whereWrap.getInstrumentid() + "")) {
							iterator.remove();
							continue;
						}
					}
				}
			} 
		}
		return list;
	}
	
//	查询resource中的数据
	public List<Resource> findAll() {
		// TODO Auto-generated method stub
		return findAll(new Resource());
	}
	
//	根据主键查询resource中的一条数据
	public Resource findById(Integer id) {
		// TODO Auto-generated method stub
		return resourceDao.selectByPrimaryKey(id);
	}

//	选择性添加resource表中的resource数据
	public int insert(Resource resource) {
		// TODO Auto-generated method stub
		return resourceDao.insertSelective(resource);
	}

//	根据主键更新resource表中的一条记录
	public int updateById(Resource resource) {
		// TODO Auto-generated method stub
		return resourceDao.updateByPrimaryKey(resource);
	}

//	根据主键选择性更新resource表中的resource数据
	public int updateByIdSelective(Resource resource) {
		// TODO Auto-generated method stub
		Resource dbResource = findById(resource.getId());
		if (resource.getId()==null) {
			resource.setId(dbResource.getId());
		}
		if (resource.getFile()==null) {
			resource.setFile(dbResource.getFile());
		}
		if (resource.getInstrumentid()==null) {
			resource.setInstrumentid(dbResource.getInstrumentid());
		}
		return updateById(resource);
	}

//	根据条件删除resource表中的数据
	public int delete(Resource whereWrap) {
		// TODO Auto-generated method stub
		return delete(whereWrap,true);
	}

//	根据条件和模式删除resource表中的数据
	public int delete(Resource whereWrap, boolean mode) {
		// TODO Auto-generated method stub
		List<Resource> list = findAll(whereWrap, mode);
		for (Resource resource : list) {
			deleteById(resource.getId());
		}
		return list.size();
	}
//	根据主键删除resource表中的一条记录
	public int deleteById(Integer id) {
		// TODO Auto-generated method stub
		return resourceDao.deleteByPrimaryKey(id);
	}

//	根据主键集合批量删除resource表中的记录
	public int deleteByIds(List<Integer> list){
		for (Integer id : list) {
			deleteById(id);
		}
		return list.size();
	}
}
